<template>
  <section>
    <!--工具条-->
    <br/>
    <el-col :span="24" class="toolbar" style="padding-left: 10px">
      <el-form :inline="true" :model="filters">
        <el-form-item >
          <el-input v-model="filters.userName" placeholder="员工名字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getEmployees">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="userName" label="员工姓名" min-width sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" min-width sortable>
      </el-table-column>
      <el-table-column prop="phone" label="电话" min-width sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" min-width sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width sortable>
        <template scope="scope">
          <span style="color: #5daf34" v-if="scope.row.state === 0">启用</span>
          <span style="color:red" v-else>禁用</span>
        </template>
      </el-table-column>


      <el-table-column label="操作" min-width>
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <br/>
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0" style="margin-left:10px">批量删除
      </el-button>
      <el-pagination
        background
        :current-page="page"
        :page-size="size"
        :total="total"
        :page-sizes="[5,10,15,20]"
        style="float:right;margin-right: 450px"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="currentChange"
        @size-change="sizeChange"
      ></el-pagination>
    </el-col>


    <!--新增界面-->
    <el-dialog :title="title" :visible.sync="empFormVisible" :close-on-click-modal="false">
      <el-form :model="empForm" label-width="80px" :rules="empFormRules" ref="empForm">
        <el-form-item label="员工名称" prop="userName" style="width: 500px;">
          <el-input v-model="empForm.userName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" style="width: 500px;">
          <el-input v-model="empForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone" style="width: 500px;">
          <el-input v-model="empForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age" style="width: 500px;">
          <el-input v-model="empForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="开启状态" prop="state">
          <el-radio-group v-model="empForm.state">
            <el-radio class="radio" :label="0">启用</el-radio>
            <el-radio class="radio" :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="empFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="deptSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import emp from '@/api/org/emp/emp'
//import NProgress from 'nprogress'

export default {
  data() {
    return {
      title: '',
      filters: {
        name: ''
      },
      departments: [],
      employees: [],
      page: 1,
      total: 0,
      size: 5,
      listLoading: false,
      sels: [],//列表选中列

      empFormVisible: false,//新增界面是否显示
      empFormRules: {
        userName: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //新增界面数据
      empForm: {
        id: null,
        userName: '',
        email: '',
        phone: '',
        age:null,
        state:0
      }

    }
  },
  methods: {

    currentChange(val) {
      this.page = val;
      this.getEmployees();
    },
    sizeChange(val) {
      this.size = val;
      this.getEmployees()
    },
    //获取部门列表
    getEmployees() {
      this.listLoading = true;//开启忙等框
      emp.listEmp(this.page, this.size, this.filters)
        .then(response => {
          let data = response.data
          this.total = data.totals
          this.employees = data.rows
          setTimeout(() => {
            this.listLoading = false
          }, 1.5 * 1000)//关闭忙等框
        })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        emp.deleteEmp(row.id)
          .then(response => {
            return this.$message({
              showClose: true,
              message: '删除成功!',
              type: 'success'
            });
          })
          .then(response => {
            this.getEmployees()
          })
          .catch(error => {
            return this.$message({
              showClose: true,
              message: '删除失败',
              type: 'error'
            })
          })
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑"
      this.empFormVisible = true;
      this.empForm = Object.assign({}, row);

    },
    //显示新增界面
    handleAdd: function () {
      this.title = "添加"
      this.empFormVisible = true;
      this.empForm = {
        id: null,
        userName: '',
        email: '',
        phone: '',
        age:null,
        state:0
      };
    },

    //新增
    deptSubmit: function () {
      this.$refs.empForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //NProgress.start();
            let para = Object.assign({}, this.empForm);
            if(para.parent){
              let parent = para.parent;
              //拿到数组的最后一个
              let pid = parent[parent.length-1];
              /*重新设置parent的值*/
              para.parent = {"id":pid};
            }
            emp.save(para)
              .then(response => {

                return this.$message({
                  showClose: true,
                  message: '操作成功!',
                  type: 'success'
                })
              })
              .then(response => {
                this.empFormVisible = false
                this.getEmployees()
              })
              .catch(error => {
                return this.$message({
                  showClose: true,
                  message: '操作失败!请稍后重试',
                  type: 'error'
                })
              })
            this.deptTree=[]

          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        emp.deleteBatch(ids)
          .then(response => {
            return this.$message({
              showClose: true,
              message: '删除成功!',
              type: 'success'
            });
          })
          .then(response => {
            this.getEmployees()
          })
          .catch(error => {
            return this.$message({
              showClose: true,
              message: '删除失败!请稍后重试',
              type: 'error'
            })
          })
      }).catch(() => {

      });
    },

  },
  mounted() {
    this.getEmployees();
  }
}

</script>

<style scoped>


</style>
